package com.cskaoyan.order.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cskaoyan.mall.commons.constant.SysRetCodeConstants;
import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.order.constant.OrderRetCode;
import com.cskaoyan.order.constant.OrderConstants;
import com.cskaoyan.order.dto.*;
import com.cskaoyan.order.form.CancelOrderForm;
import com.cskaoyan.order.form.OrderDetail;
import com.cskaoyan.order.form.PageInfo;
import com.cskaoyan.order.service.OrderCoreService;
import com.cskaoyan.order.service.OrderQueryService;
import com.cskaoyan.order.utils.GlobalIdGeneratorUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @author 苏东宇
 * @since 2022/07/29 15:49
 */
@RestController
@RequestMapping("/shopping")
public class OrderController {

    @Autowired
    OrderCoreService orderCoreService;

    @Autowired
    OrderQueryService orderQueryService;

    @Autowired
    GlobalIdGeneratorUtil globalIdGeneratorUtil;

    /**
     * 下单
 * @param request
 * @param servletRequest
     * @return com.cskaoyan.mall.commons.result.ResponseData
     * @author 苏东宇
     * @since 2022/07/30 15:57
     */
    @PostMapping("/order")
    public ResponseData createOrder(@RequestBody CreateOrderRequest request, HttpServletRequest servletRequest) throws IOException {

        //从token中获取用户信息
        String user_info = (String) servletRequest.getHeader("user_info");
        JSONObject object = JSON.parseObject(user_info);
        long uid = Long.parseLong(object.get("uid").toString());


        //设置uniqueKey
        String nextSeq = globalIdGeneratorUtil.getNextSeq("uniqueKey", 1);

        request.setUniqueKey(nextSeq);
        request.setUserId(uid);


        //调用创建订单方法
        CreateOrderResponse response = orderCoreService.createOrder(request);

        if (response.getCode().equals(OrderRetCode.SUCCESS.getCode())){
            return new ResponseUtil<>().setData(response.getOrderId());
        }

        return new ResponseUtil<>().setErrorMsg(response.getMsg());
    }


    /**
     * 获取用户全部订单
 * @param pageInfo
     * @return com.cskaoyan.mall.commons.result.ResponseData
     * @author 苏东宇
     * @since 2022/07/30 16:17
     */
    @GetMapping("/order")
    public ResponseData allOfUserOrders(PageInfo pageInfo,HttpServletRequest servletRequest){
        //从token中获取用户信息
        String user_info = (String) servletRequest.getHeader("user_info");
        JSONObject object = JSON.parseObject(user_info);
        long uid = Long.parseLong(object.get("uid").toString());

        OrderListRequest orderListRequest = new OrderListRequest();
        orderListRequest.setUserId(uid);
        orderListRequest.setPage(pageInfo.getPage());
        orderListRequest.setSize(pageInfo.getSize());
        orderListRequest.setSort(pageInfo.getSort());

        OrderListResponse orderListResponse = orderQueryService.orderList(orderListRequest);

        if (OrderRetCode.SUCCESS.getCode().equals(orderListResponse.getCode())){
            return new ResponseUtil<>().setData(orderListResponse);
        }

        return new ResponseUtil<>().setErrorMsg(orderListResponse.getMsg());
    }




    /**
     *
     * @param orderId
     * @param request
     * @return com.cskaoyan.mall.commons.result.ResponseData
     * @author 苏东宇
     * @since 2022/07/30 17:00
     */
    @GetMapping("/order/{id}")
    public ResponseData queryOrderInfo(@PathVariable("id")String orderId, HttpServletRequest request){
        OrderDetailRequest orderDetailRequest = new OrderDetailRequest();
        orderDetailRequest.setOrderId(orderId);

        OrderDetail orderDetail = orderQueryService.orderDetail(orderDetailRequest);

        if (OrderRetCode.SUCCESS.getCode().equals(orderDetail.getCode())){
            return new ResponseUtil<>().setData(orderDetail);
        }


        return new ResponseUtil<>().setErrorMsg(orderDetail.getMsg());
    }

    @PostMapping("/cancelOrder")
    public ResponseData cancelOrder(@RequestBody CancelOrderForm cancelOrderForm, HttpServletRequest request){
        // 判断是否携带Token
//        String userInfo = request.getHeader("user_info");
//        if(userInfo == null){
//            return new ResponseUtil().setErrorMsg("请先登录");
//        }

        // 携带了Token
        // 执行更新操作
        CancelOrderRequest cancelOrderRequest = new CancelOrderRequest();
        cancelOrderRequest.setOrderId(cancelOrderForm.getOrderId());

        CancelOrderResponse cancelOrderResponse = orderCoreService.cancelOrder(cancelOrderRequest);
        if(!cancelOrderResponse.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())){
            return new ResponseUtil().setErrorMsg("取消失败，订单号不存在");
        }
        return new ResponseUtil().setData(cancelOrderResponse);
    }

    @DeleteMapping("/order/{id}")
    public ResponseData deleteOrder(@PathVariable("id")String orderId,HttpServletRequest request){

        DeleteOrderRequest deleteOrderRequest = new DeleteOrderRequest();
        deleteOrderRequest.setOrderId(orderId);
        DeleteOrderResponse deleteOrderResponse = orderCoreService.deleteOrder(deleteOrderRequest);
        if(!deleteOrderResponse.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())){
            return new ResponseUtil().setErrorMsg("删除失败，订单号不存在");
        }

        return new ResponseUtil().setData(deleteOrderResponse);
    }

}
